let _POOL = {};
export class MyMeshQuadTexture extends Laya.Mesh2D {
    private static _defines = {};
    public const_stride: number = 24;
    private static _fixib = null;
    public destroyed = false;
    public released = false;
    private meshName: string = "";
    constructor(meshName, stride, attributes) {
        super(stride, 4, 4);
        this.meshName = meshName;
        let a = [];
        let startIdx = 0;
        for (let i = 0; i < attributes.length; i++) {
            let attri = attributes[i];
            a.push(attri[1]);
            a.push(attri[2]);
            a.push(startIdx);
            startIdx += attri[2] * 4;
        }
        this.const_stride = stride;

        this.setAttributes(a);
        this.canReuse = true;
        if (!MyMeshQuadTexture._fixib) {
            this.createQuadIB(16 * 1024);
            MyMeshQuadTexture._fixib = this._ib;
        } else {
            this._ib = MyMeshQuadTexture._fixib;
            this._quadNum = 16 * 1024;
        }

    }


    static getAMesh(meshName, attributes, mainctx: boolean): MyMeshQuadTexture {
        var ret = null;
        if (_POOL[meshName] && _POOL[meshName].length > 0) {
            ret = _POOL[meshName].pop();
            ret.released = false;
        }
        else {
            let stride = 0;
            for (let i = 0; i < attributes.length; i++) {
                let attri = attributes[i];
                stride += attri[2] * 4;
            }
            ret = new MyMeshQuadTexture(meshName, stride, attributes);
        }
        mainctx && ret._vb._resizeBuffer(64 * 1024 * ret.const_stride, false);
        return ret;
    }

    releaseMesh(): void {
        this.released = true;
        this._vb.setByteLength(0);
        this.vertNum = 0;
        this.indexNum = 0;
        if (!_POOL[this.meshName]) {
            _POOL[this.meshName] = [];
        }
        _POOL[this.meshName].push(this);
    }
    destroy() {
        this._vb.destroy();
        this._vb.deleteBuffer();
        this.destroyed = true;
    }
}